import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/image_util.dart';
import 'package:framelib/utils/size_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
/**
 * author：twopai
 * 基于SmartRefresher的封装：下拉刷新、上拉加载更多、空视图、错误视图、重新加载
 * 1.空视图的时候只能下拉刷新，不能上拉
 * 2.错误视图的时候只能重新加载，不能上拉和下拉
 */
class RefreshWidget{
  static init(
      RefreshController refreshController,Widget childWidget,
      {
        Key? key,
        required bool enablePullDown,
        required bool enablePullUp,
        required RefreshState? loadStatus,
        required bool isCanMoreData,
        required VoidCallback onRefresh,
        required VoidCallback onLoading,
        required VoidCallback? onReFreshFunction,
        bool? reverse,
        ScrollPhysics? physics,
        Widget? errorWidget,
        Widget? emptyWidget,
        Widget? notNetWidget,
        Color? refreshFontColor,
        ScrollController? scrollController,
      }){
    if(loadStatus==null)loadStatus=RefreshState.INIT;
    return SmartRefresher(
      key: key,
      reverse: reverse,
      scrollController: scrollController,
      controller: refreshController,
      enablePullDown: (loadStatus==RefreshState.INIT||loadStatus==RefreshState.OK||loadStatus==RefreshState.EMPTY)?(enablePullDown??false):false,
      enablePullUp: (loadStatus==RefreshState.OK&&isCanMoreData==true)?(enablePullUp??false):false,
        physics: physics??ClampingScrollPhysics(),
      child:_setChildUi(loadStatus,childWidget, errorWidget, emptyWidget, notNetWidget,onReFreshFunction),
     header: CustomHeader(
       builder: (BuildContext context,RefreshStatus? mode){
         Widget body ;
         if(mode==RefreshStatus.idle){
           body =  Text(pull_down_refresh,
             style: TextStyle(
             color: refreshFontColor??Colors.white
            ),
           );
         }
         else if(mode==RefreshStatus.refreshing){
           body =  CupertinoActivityIndicator();
         }
         else if(mode == RefreshStatus.failed){
           body = Text(refresh_again,
               style: TextStyle(
               color: refreshFontColor??Colors.white
         ),
           );
         }
         else if(mode == RefreshStatus.canRefresh){
           body = Text(release_refresh,
             style: TextStyle(
                 color: refreshFontColor??Colors.white
             ),
           );
         }
         else{
           body = Text(release_success,
             style: TextStyle(
                 color: refreshFontColor??Colors.white
             ),
           );
         }
         return Container(
           height: size_55,
           child: Center(child:body),
         );
       },
     ),
      footer: CustomFooter(
        builder: (BuildContext context,LoadStatus? mode){
          Widget body ;
          if(mode==LoadStatus.idle){
            body =  Text("");
          }
          else if(mode==LoadStatus.loading){
            body =  CupertinoActivityIndicator();
          }
          else if(mode == LoadStatus.failed){
            body = Text(load_fail_reclick,style: TextStyle(
                color: Colors.white
            ),);
          }
          else if(mode == LoadStatus.canLoading){
            body = Text(release_load_more,style: TextStyle(
                color: Colors.white
            ),);
          }
          else{
            body = Text(no_more_data,style: TextStyle(
                color: Colors.white
            ),);
          }
          return Container(
            height: size_30.h,
            child: Center(child:body),
          );
        },
      ),
      onRefresh: onRefresh,
      onLoading:onLoading,
    );
}

  static _setChildUi(
      RefreshState? loadStatus,
      Widget childWidget,
      Widget? errorWidget,
      Widget? emptyWidget,
      Widget? notNetWidget,
      VoidCallback? onReFreshFunction
      ) {
    if(loadStatus==null){
      loadStatus=RefreshState.EMPTY;
    }
    Widget childUi=
            Container(
              child: Center(
                child: Text(''),
              ),
            );
    switch(loadStatus){
      case RefreshState.NOT_NET:
        // TODO: Handle this case.
        childUi=
            notNetWidget==null?
            Container(
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
//                    ImageUtils.loadAssetImage(
//                        DrawableUtils.neterror_img,
//                        width: PxUtils.setSize(ScreenUtil.getInstance().screenWidth*2/3),
//                        fit: BoxFit.fitWidth
//                    ),
                    Container(
                      margin: EdgeInsets.only(
                          top: size_5.h,
                      ),
                      child: Text(
                          '${net_error}',
                        style: TextStyle(
                            color: Colors.white
                        ),
                      ),
                    ),
                    InkWell(
                      child: Container(
                        margin: EdgeInsets.only(
                          top:size_39d5.h,
                          left:size_91.w,
                          right:size_91.w,
                        ),
                        alignment: Alignment.center,
                        height: size_35.h,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(size_17d5)),
                            gradient: LinearGradient(
                                colors: [
                                  color_blue,
                                  color_blue,
                                ]
                            )
                        ),
                        child: Text(
                          click_refresh,
                          style: TextStyle(
                              fontSize: size_15.sp,
                              color: color_white,
                              fontWeight: FontWeight.w500,
                              fontFamily: 'PingFang-SC-Medium'
                          ),
                        ),
                      ),
                      onTap:()=>((onReFreshFunction==null)?((){}):onReFreshFunction()),
                    )
                  ],
                ),
              ),
            ):notNetWidget;
        break;
      case RefreshState.NET_ERROR:
        // TODO: Handle this case.
        childUi=
            errorWidget==null?
            Container(
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
//                    ImageUtils.loadAssetImage(
//                        DrawableUtils.neterror_img,
//                        width: PxUtils.setSize(ScreenUtil.getInstance().screenWidth*2/3),
//                        fit: BoxFit.fitWidth
//                    ),
                    Container(
                      margin: EdgeInsets.only(
                          top: size_5.h
                      ),
                      child: Text(
                          '${net_error}',
                        style: TextStyle(
                          color: Colors.white
                        ),
                      ),
                    ),
                    InkWell(
                      child: Container(
                        margin: EdgeInsets.only(
                          top:size_39d5.h,
                          left:size_91.w,
                          right:size_91.w,
                        ),
                        alignment: Alignment.center,
                        height: size_35,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(size_17d5)),
                            gradient: LinearGradient(
                                colors: [
                                  color_blue,
                                  color_blue,
                                ]
                            )
                        ),
                        child: Text(
                          click_refresh,
                          style: TextStyle(
                              fontSize: size_15.sp,
                              color: color_white,
                              fontWeight: FontWeight.w500,
                              fontFamily: 'PingFang-SC-Medium'
                          ),
                        ),
                      ),
                      onTap:(onReFreshFunction==null)?null:()=> onReFreshFunction(),
                    )
                  ],
                ),
              ),
            ):errorWidget;
        break;
      case RefreshState.EMPTY:
        // TODO: Handle this case.
        childUi=
            emptyWidget==null?
            Container(
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    ImageUtil.loadImage(
                        drawable_no_data_img,
                       width: size_screen_width.w*1/2,
                       fit: BoxFit.fitWidth
                   ),
                  ],
                ),
              ),
            ):emptyWidget;
        break;
      case RefreshState.OK:
        // TODO: Handle this case.
        childUi=childWidget==null?
            Container(
              child: Center(
                child: Text(
                    has_data,
                  style: TextStyle(
                      color: Colors.white
                  ),
                ),
              ),
            ):childWidget;
        break;
      case RefreshState.INIT:
      // TODO: Handle this case.
        childUi=
            Container(
              child: Center(
                child: Text(''),
              ),
            );
        break;
      case RefreshState.LOADING:
      // TODO: Handle this case.
        childUi=
            Container(
              child: Center(
                child: Text(
                    data_loading,
                  style: TextStyle(
                      color: Colors.white
                  ),
                ),
              ),
            );
        break;
    }
    return childUi;
  }
}

enum RefreshState{
  NOT_NET,
  NET_ERROR,
  EMPTY,
  OK,
  INIT,
  LOADING,
}
